package com.gsx.mapper;

import com.gsx.entity.Blog_Type_User;
import com.gsx.entity.Tag;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TagMapper {
    @Insert("insert into tag(tag_id,tag_name) values(#{tag_id},#{tag_name})")
    public int saveTag(Tag tag);

    @Select("select * from tag where tag_id=#{tag_id}")
    public Tag getTag(Long id);

    @Select("select * from tag")
    public List<Tag> getListTag();

    @Update("update tag set tag_name=#{tag_name} where tag_id=#{tag_id}")
    public int updateTag(Tag tag);

    @Delete("delete from tag where tag_id=#{tag_id}")
    public int deleteTag(Long id);

    @Select("select * from tag")
    public List<Tag> getAll();

    @Select("select * from tag where tag_name=#{tag_name}")
    public Tag getTagByName(String tag_name);



    /*根据blog_id来查询blog_id表中的数据进行crud操作*/
    @Insert("insert into blog_tag(blog_id,tag_id) values(#{blog_id},#{tag_id})")
    public int bt_idInsert(@Param("blog_id") Long blog_id,@Param("tag_id") Long tag_id);

    @Delete("delete from blog_tag where blog_id=#{blog_id}")
    public int delBlog_tag(Long blog_id);

    @Select("SELECT t.tag_id,t.tag_name  FROM blog_tag AS bt INNER JOIN tag AS t ON bt.tag_id=t.tag_id WHERE bt.blog_id=#{blog_id}")
    public List<Tag> getBlog_tag(Long blog_id);

    /*重写五表联查的sql语句  但是不用增加数据，只用添加一个限制条件就行*/
    @Select("SELECT b.blog_id,b.title,b.content,b.description,b.firstPicture,b.views,b.appreciation,b.shareStatement,b.commentable,b.published,b.recommend,b.updateTime,b.flag,t.type_name,t.type_id,u.user_id,u.nickname,u.avatar  " +
            " FROM blog AS b INNER JOIN USER AS u ON b.user_id=u.user_id " +
            " INNER JOIN TYPE AS t ON b.type_id=t.type_id  "+
            " INNER JOIN blog_tag AS bt ON bt.blog_id=b.blog_id "
            +" INNER JOIN tag AS g ON bt.tag_id=g.tag_id WHERE g.tag_id=#{id} ")
    public List<Blog_Type_User> getBlog_Tags(Long tag_id);

}
